package com.tutu.controller.backend;

import com.tutu.common.DataTableFactory;
import com.tutu.controller.common.CommonController;
import com.tutu.service.MasterService;
import com.tutu.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 账号管理
 */
@Controller
@RequestMapping(value = "backend/master")
public class MasterController extends CommonController {

    @Autowired
    private MasterService masterService;

    @RequestMapping(value = "/index")
    public String index() {
        return "backend/master/list";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response, Integer companyId, Integer status, String keyWord, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, masterService.pageByParams(companyId, status, keyWord, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(HttpServletRequest request, Integer id, Integer roleId, Integer companyId, String account, String name, String password, String mobile, String email, Integer status) {
        return masterService.saveInfo(request, id, roleId, companyId, account, name, password, mobile, email, status);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> del(HttpServletRequest request, Integer id) {
        return masterService.deleteById(request, id);
    }

    @RequestMapping("/batchDel")
    @ResponseBody
    public Map<String, Object> batchDel(HttpServletRequest request, String ids) {
        return masterService.batchDel(request, ids);
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public Map<String, Object> updateStatus(HttpServletRequest request, Integer id, Integer status) {
        return masterService.updateStatus(request, id, status);
    }

    @RequestMapping("/modifyPwd")
    @ResponseBody
    public Map<String, Object> modifyPwd(HttpServletRequest request, Integer id) {
        return masterService.modifyMasterPwd(request, id);
    }
}
